package com.qidianit.mes.masterdata.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qidianit.mes.common.pojo.MesResult;
import com.qidianit.mes.masterdata.service.MaterialCategoryService;
import com.qidianit.mes.pojo.MaterialCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @company 起点编程
 * @author 戴着假发的程序员
 * @date 2025/7/22
 * @description 物料类别Controller
 */
@RestController
@RequestMapping("/materialCategories")
//@CrossOrigin
public class MaterialCategoryController {

    @Autowired
    private MaterialCategoryService materialCategoryService;
    /**
     * 新增物料类别
     */
    @PostMapping
    public MesResult<Boolean> createMaterialCategory(@RequestBody MaterialCategory materialCategory) {
        return MesResult.success(materialCategoryService.save(materialCategory));
    }




    @GetMapping("/listByParentId/{parentId}")
    public MesResult<List<MaterialCategory>> listByParentId(@PathVariable Long parentId) {
        QueryWrapper<MaterialCategory> queryWrapper = new QueryWrapper<>();
        if(parentId == null){
            parentId = 0L;
        }
        queryWrapper.eq("parent_category_id", parentId);
        return MesResult.success(materialCategoryService.list(queryWrapper));
    }

    /**
     * 查询所有物料类别
     */
    @GetMapping
    public MesResult<List<MaterialCategory>> getAllMaterialCategories() {
        return MesResult.success(materialCategoryService.list());
    }

    /**
     * 根据ID查询物料类别
     */
    @GetMapping("/{id}")
    public MesResult<MaterialCategory> getMaterialCategoryById(@PathVariable Long id) {
        return MesResult.success(materialCategoryService.getById(id));
    }


    /**
     * 更新物料类别
     */
    @PutMapping("/{id}")
    public MesResult<Boolean> updateMaterialCategory(@PathVariable Long id, @RequestBody MaterialCategory materialCategory) {
        materialCategory.setMaterialCategoryId(id);
        return MesResult.success(materialCategoryService.updateById(materialCategory));
    }

    /**
     * 删除物料类别
     */
    @DeleteMapping("/{id}")
    public MesResult<Boolean> deleteMaterialCategory(@PathVariable Long id) {
        return MesResult.success(materialCategoryService.removeById(id));
    }
}